package algorithm_demo.demo01;

/**
 * 二叉树的遍历：先序遍历、中序遍历、后序遍历
 * 先序：头 左 右
 * 中序：左 头 右
 * 后序：左 右 头
 *
 * @author Api
 * @date 2023/2/4 11:48
 */
public class Code23_TraversalBinaryTree {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int v) {
            value = v;
        }
    }

    /*先序遍历：打印所有节点*/
    public static void pre(Node head){
        if (head == null){
            return;
        }
        System.out.print(head.value+" ");
        pre(head.left);
        pre(head.right);
    }

    /*中序遍历：打印所有节点*/
    public static void in(Node head){
        if (head == null){
            return;
        }
        in(head.left);
        System.out.print(head.value+" ");
        in(head.right);
    }


    /*后序遍历：打印所有节点*/
    public static void pos(Node head){
        if (head == null){
            return;
        }
        pos(head.left);
        pos(head.right);
        System.out.print(head.value+" ");
    }


    public static void main(String[] args) {
        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);
        head.left.left = new Node(4);
        head.left.right = new Node(5);
        head.right.left = new Node(6);
        head.right.right = new Node(7);

        pre(head);
        in(head);
        pos(head);

    }











}
